package com.app.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.app.model.Menu;
import com.app.resp.MenuResponse;

@Repository
public class MenuDao {
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	public List<MenuResponse> findAll(){
		List<MenuResponse> result = new ArrayList<MenuResponse>();
		String menuSql = "select * from auth_menu where pId is null"; 
		List<Menu> menuList = jdbcTemplate.query(menuSql,new BeanPropertyRowMapper(Menu.class));
		
		for(Menu item:menuList){
			MenuResponse resp = new MenuResponse();
			String subMenuSql = "select * from auth_menu where pId = ?"; 
			Object args[] = new Object[]{item.getId()};  
			List<Menu> subList = jdbcTemplate.query(subMenuSql,args,new BeanPropertyRowMapper(Menu.class)); 
			resp.setName(item.getName());
			resp.setId(item.getId());
			resp.setSub(subList);
			result.add(resp);
		}
        return result;  
	}

	public Map<String,String> findMenu(String url){
		Map<String,String> map = new HashMap<String,String>();
		String subSql = "select * from auth_menu where url = ?"; 
		Object args1[] = new Object[]{url};  
		Menu sub = jdbcTemplate.queryForObject(subSql, args1, new BeanPropertyRowMapper(Menu.class));
		
		String parentSql = "select * from auth_menu where id = ?"; 
		Object args2[] = new Object[]{sub.getpId()};  
		Menu parent = jdbcTemplate.queryForObject(parentSql, args2, new BeanPropertyRowMapper(Menu.class));
		map.put("menu", parent.getName());
		map.put("menuname", sub.getName());
		return map;
	}
	
	public List<Menu> findAllByPage(String sql){
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(Menu.class));
	}
	
	public List<Menu> findAllByTree(){
		String sql = "select * from auth_menu"; 
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(Menu.class));
	}

	public Integer getCount(String sql) {
		return jdbcTemplate.queryForObject(sql, Integer.class);
	}

	public void save(Menu menu) {
		String sql = "insert into auth_menu(name) values(?)";  
        Object args[] = {menu.getName()};  
        jdbcTemplate.update(sql, args);  
	}
	
	public void update(Menu menu) {
		String sql = "update auth_menu set name = ? where id = ?";  
        Object args[] = {menu.getName(),menu.getId()};  
        jdbcTemplate.update(sql, args);  
	}
	
	//删除  
    public void delete(Long id) {  
        String sql = "delete from auth_menu where id = ?";  
        Object args[] = new Object[]{id};  
        jdbcTemplate.update(sql,args);  
    }

	public Menu getById(Long id) {
		String sql = "select * from auth_menu where id = ?";  
        Object args[] = new Object[]{id};  
        Menu  menu = jdbcTemplate.queryForObject(sql,args,new BeanPropertyRowMapper(Menu.class));  
        return menu;  
	}

	public List<Menu> getParentMenu() {
		String menuSql = "select * from auth_menu where pId is null"; 
		List<Menu> menuList = jdbcTemplate.query(menuSql,new BeanPropertyRowMapper(Menu.class));
		return menuList;
	}

	public List<Menu> getSubMenu(Long pid) {
		String menuSql = "select * from auth_menu where pId = ?"; 
		Object args[] = new Object[]{pid};  
		List<Menu> menuList = jdbcTemplate.query(menuSql,args,new BeanPropertyRowMapper(Menu.class));
		return menuList;
	}  
	
}
